package zsrt.com.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import zsrt.com.dao.ExamineeDao;

public class MapSort {
	/**
	 * 排序Map
	 * @param map	需要排序的容器
	 * @return
	 */
	public static HashMap<String, ExamineeDao> mapSortDesc(Map<String, ExamineeDao> map) {
	    LinkedHashMap<String, ExamineeDao> newHash = new LinkedHashMap<>();
	    Set<Entry<String, ExamineeDao>> tempSet = map.entrySet();
	    ArrayList<Entry<String, ExamineeDao>> tempList = new ArrayList<Entry<String, ExamineeDao>>(tempSet);
	    Collections.sort(tempList, new Comparator<Entry<String, ExamineeDao>>() {
	        @Override
	        public int compare(Entry<String, ExamineeDao> o1, Entry<String, ExamineeDao> o2) {
	            return (int) (o2.getValue().getTotal() - o1.getValue().getTotal());
	        }
	    });
	    for(int i = 0;i < tempList.size();i ++) {
	        newHash.put(tempList.get(i).getKey(), tempList.get(i).getValue());
	    }
	    return newHash;
    }
	
	public static HashMap<String, ExamineeDao> mapSortOrderBy(Map<String, ExamineeDao> map) {
	    LinkedHashMap<String, ExamineeDao> newHash = new LinkedHashMap<>();
	    Set<Entry<String, ExamineeDao>> tempSet = map.entrySet();
	    ArrayList<Entry<String, ExamineeDao>> tempList = new ArrayList<Entry<String, ExamineeDao>>(tempSet);
	    Collections.sort(tempList, new Comparator<Entry<String, ExamineeDao>>() {
	        @Override
	        public int compare(Entry<String, ExamineeDao> o1, Entry<String, ExamineeDao> o2) {
	            return (int) (o1.getValue().getTotal() - o2.getValue().getTotal());
	        }
	    });
	    for(int i = 0;i < tempList.size();i ++) {
	        newHash.put(tempList.get(i).getKey(), tempList.get(i).getValue());
	    }
	    return newHash;
    }
}
